package com.blb.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.community.entity.SysUser;
import com.blb.community.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @Author 帅帅
 * @Date 2022/7/4 15:09
 * @PackageName:com.blb.community.service.impl
 * @ClassName: UserDetailsServiceImpl
 * @Description: springSecurity用户登入验证
 * @Version 1.0
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = sysUserMapper.selectOne(new QueryWrapper<SysUser>().lambda().eq(SysUser::getUserName, username));
        if (sysUser == null) {
            throw new UsernameNotFoundException("没有该用户");
        }
        return new User(username, sysUser.getPassword(), AuthorityUtils.commaSeparatedStringToAuthorityList(""));
    }
}
